from datetime import datetime
import string
# 可生成农历1901-2098的数据
# 录入农历1-1899的数据

CHINESENUMBERS = {
    '一': 1,
    '二': 2,
    '三': 3,
    '四': 4,
    '五': 5,
    '六': 6,
    '七': 7,
    '八': 8,
    '九': 9,
    '十': 10,
    '正': 1
}


def read_single_file(file_name, coding="big5"):
    result = list()
    with open(file_name, encoding=coding) as file:
        for idx, l in enumerate(file.readlines()):
            if idx < 3:
                continue
            else:
                result.append(
                    list(filter(lambda x: x != "" and x != "\n", l.split(" "))))
    return result


def day_data_process(day_data, c_year, c_month, c_leap=False):
    day_info = dict()
    date = datetime.strptime(day_data[0], '%Y年%m月%d日')
    day_info['year'] = date.year
    day_info['month'] = date.month
    day_info['day'] = date.day

    chinese_day = day_data[1]
    if chinese_day == '正月':
        day_info['lunar_year'] = c_year + 1
    else:
        day_info['lunar_year'] = c_year

    if chinese_day[-1] == '月':
        if chinese_day[0] == '閏':
            day_info['lunar_leap'] = True
            if len(chinese_day) == 4:
                day_info['lunar_month'] = 10 + CHINESENUMBERS[chinese_day[2]]
            else:
                day_info['lunar_month'] = CHINESENUMBERS[chinese_day[1]]
        else:
            day_info['lunar_leap'] = False
            if len(chinese_day) == 3:
                day_info['lunar_month'] = 10 + CHINESENUMBERS[chinese_day[1]]
            else:
                day_info['lunar_month'] = CHINESENUMBERS[chinese_day[0]]
        day_info['lunar_day'] = 1
    else:
        day_info['lunar_month'] = c_month
        day_info['lunar_leap'] = c_leap

        if chinese_day[0] == '初':
            day_info['lunar_day'] = CHINESENUMBERS[chinese_day[1]]
        elif chinese_day[0] == '十':
            day_info['lunar_day'] = 10 + CHINESENUMBERS[chinese_day[1]]
        elif chinese_day[0] == '廿':
            day_info['lunar_day'] = 20 + CHINESENUMBERS[chinese_day[1]]
        elif chinese_day == '二十':
            day_info['lunar_day'] = 20
        elif chinese_day == '三十':
            day_info['lunar_day'] = 30

    return day_info


def lunar_data():
    data_list = list()
    for i in range(1901, 2101):
        data_list = data_list + read_single_file(f"T{i}c.txt")
    lunar_calendar_data = list()
    for day in data_list:
        try:
            datetime.strptime(day[0], '%Y年%m月%d日')
        except:
            continue
        if len(lunar_calendar_data) != 0:
            lunar_calendar_data.append(
                day_data_process(
                    day, lunar_calendar_data[-1]['lunar_year'], lunar_calendar_data[-1]['lunar_month'], lunar_calendar_data[-1]['lunar_leap'])
            )
        else:
            lunar_calendar_data.append(day_data_process(day, 1900, 11))

    return(lunar_calendar_data)  # 返回一个每天日期信息字典的List


# 我生成的转码表似乎有问题,比如农历2096年我的是47572,他的是43476,核对txt文件发现大佬的才是的是对的,农历2096年的4月是29天.(2057 2063 2074 2077 2090 2096都不同)

arr = lunar_data()
arr1 = []  # 存放的月份天数(x月与润x月是合并的),如[29,30,29,59,29,.......]
arr2 = []  # 存放闰x月前一个月的天数,如[29],可知这个月后面的闰月天数为59-29等于30
arr3 = []  # 存放最后转换的码
arr4 = []
arr5 = []
X = 0
II = 0
ii = 0
code = 1
R = 0
L = 0

while arr[X]["lunar_year"] != 1901:
    X += 1
i = X

for Y in range(1901, 2101):
    for M in range(1, 13):
        I = 0
        while arr[i]["lunar_year"] == Y and arr[i]["lunar_month"] == M:
            i += 1
            I += 1
            if arr[i-1]["lunar_leap"] != True and arr[i]["lunar_leap"] == True:
                arr2.append(I)
            if arr[i]["lunar_month"] != M:
                arr1.append(I)
            if arr[i]["lunar_year"] == 2100 and arr[i]["lunar_month"] == 11 and arr[i]["lunar_day"] == 30:
                break    # 要在2100.11.30设置一个断点,否則IndexError: list index out of range


# 最多到2099,否则IndexError: list index out of range
for y in range(1901, 2099):
    R = 0
    for m in range(1, 13):
        if arr1[ii] == 29:
            code = code << 1
            ii += 1

        elif arr1[ii] == 30:
            code = (code << 1) + 1
            ii += 1

        elif arr2[II] == 29:
            code = code << 1
            R = m
            L = arr1[ii] - arr2[II]
            II += 1
            ii += 1

        elif arr2[II] == 30:
            code = (code << 1) + 1
            R = m
            L = arr1[ii] - arr2[II]
            II += 1
            ii += 1

    code = (code & 4095) << 4
    if R != 0:
        code = code + R
        if L == 30:
            code = code + 65536

    arr3.append(code)

    arr4 = [1010101101010000, 1010101010110000, 101010101010000, 101101010100000,
            1010111010100101, 1010101010110000, 101010101010000, 0, 1010101101010000, 101010101111010,
            101010101010000, 110101010100000, 1010101110101000, 1010101011010000, 101010101010000,
            110110101010101, 101011010100000, 1010101011010000, 10010101010100001, 1101010101010000,
            101101011011001, 101010110100000, 11010101010011010, 1010110101010101, 110101010100000,
            1010101101010000, 110101011010010, 101010101010000, 10110101010011010, 101011010100000,
            1010101011010000, 101011010100110, 1101010101010000, 101101010100000, 1011010110100011,
            1010101010100000, 1101010101011100, 1011010101010000, 101010110100000, 1010101011100000,
            1010101010100000, 1011010101010000, 101101101010100, 101010101100000, 1010101010100000,
            11110101010100001, 1010110101010000, 10101010101011010, 101010101010000, 110101010100000,
            1010111010100110, 1010101011010000, 101010101010000, 1011010101010011, 0101101010100000,
            1010101011011100, 1010101010110000, 101010101010000, 101101011011001, 101011010100000,
            1010101010110000, 10101001010100101, 1011010101010000, 101011010100000, 1101010101100001,
            1010101010100000, 1101010101101010, 1010110101010000, 101010101100000, 1010101101010111,
            1010101010100000, 1010110101010000, 10100101101010011, 101010101010000, 1010101010111011,
            101101010100000, 1010101101010000, 101010110111000, 101010101010000, 110101010100000,


            ]

    for i in arr4:
        b = str(arr4[ii])
        arr5.append(int(b, 2))
        ii += 1

        # 最后使用列表拼接 arr3 += arr5
print(arr3)
